package mozi.com.oneday.Fragment;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import mozi.com.oneday.R;
import mozi.com.oneday.gson.User;
import mozi.com.oneday.indexActivity;
import mozi.com.oneday.utils.HttpUtil;
import mozi.com.oneday.utils.Utility;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by 执子一白 on 2017/9/22.
 */

public class LoginFragment extends Fragment {

    private static final String TAG = "LoginFragment";
    private Context mContext;



    @Bind(R.id.login_progress)
    ProgressBar loginProgress;
    @Bind(R.id.email_sign_in_button)
    Button emailSignInButton;
    @Bind(R.id.return_button)
    Button returnButton;


    WelcomeFragment welcomeFragment;
    @Bind(R.id.editText)
    EditText editText;
    @Bind(R.id.editText2)
    EditText editText2;
    @Bind(R.id.textInputLayoutName)
    TextInputLayout textInputLayoutName;
    @Bind(R.id.textInputLayoutPassword)
    TextInputLayout textInputLayoutPassword;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_login, container, false);
        ButterKnife.bind(this, view);
        this.mContext = getActivity();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.email_sign_in_button, R.id.return_button})
    public void onViewClicked(View view) {
        FragmentManager fm = getFragmentManager();
        // 开启Fragment事务
        FragmentTransaction transaction = fm.beginTransaction();
        switch (view.getId()) {
            case R.id.email_sign_in_button:
                break;
            case R.id.return_button:
                if (welcomeFragment == null) {
                    welcomeFragment = new WelcomeFragment();
                }
                // 使用当前Fragment的布局替代id_content的控件
                transaction.replace(R.id.id_content, welcomeFragment);
                break;
        }
        transaction.commit();
    }
    int code=0;
    @OnClick(R.id.email_sign_in_button)
    public void onViewClicked() {


                Log.d(TAG, "onViewClicked: 登录被点击");
                String email=editText.getText().toString();
                String password=editText2.getText().toString();
                Log.d(TAG, "onViewClicked: email="+email+"password="+password);
                sendRequestWithOkHttp(email,password);
                Log.d(TAG, "sendRequestWithOkHttp: codeout==>"+code);
                if (code!=100)
                {
                    Toast.makeText(mContext,"账号或密码错误2333",Toast.LENGTH_SHORT).show();
                }


    }

    private void sendRequestWithOkHttp(final String email, final String password){

                Log.d(TAG, "sendRequestWithOkHttp: 进入函数");

                String userUrl="http://47.93.0.157:8080/OneDay/login?email="+email+"&password="+password;
                Log.d(TAG, "run: url==>"+userUrl);
                HttpUtil.sendOkHttpRequest(userUrl,new okhttp3.Callback(){

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String responseText =response.body().string();
                        User user= Utility.handleWeatherResponse(responseText);
                        Log.d(TAG, "onResponse: "+user.toString());
                        Log.d(TAG, "onResponse: "+responseText);
                        code=user.getCode();
                        Log.d(TAG, "onResponse: code==>"+code);
                        if (user.getCode()==100){
                            Intent intent =new Intent(getActivity(), indexActivity.class);
                            startActivity(intent);
                        }else {
                            //Toast.makeText(mContext,"账号或密码错误",Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();

                                //Toast.makeText(mContext,"登录失败",Toast.LENGTH_SHORT).show();


                    }
                });




    }
}
